<?php


namespace App\Http\Controllers\Api\Driver;


use App\Exports\CarDriverExport;
use App\Exports\DriverExports;
use App\Http\Controllers\Controller;
use App\Http\Requests\DriverRequest;
use App\Http\Requests\PasswordRequest;
use App\Http\Requests\StateRequest;
use App\Models\CompanyManagement;
use App\Repositories\CarRepository;
use App\Repositories\DriverRepository;
use App\Services\DriverService;
use App\Transforms\DriverTransformer;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class DriverController extends Controller
{

    /**
     * 司机列表
     * @param Request $request
     * @param DriverRepository $repository
     * @return DriverController|JsonResponse
     * @Author 姿势就是力量
     */
    public function getPageList(Request $request, DriverRepository $repository)
    {
        $realName = (string)$request->get('real_name', '');  //真实姓名
        $phone = (string)$request->get('phone', '');         //电话
        $identity = (string)$request->get('identity', '');    //身份证
        $companyId = (int)$request->get('company_id', '');   //公司id
        $status = (string)$request->get('status', '');       //状态
        $number = (string)$request->get('number', '');       //车牌号
        $province = (int)$request->get('province', '');   //接单省id
        $city = (int)$request->get('city', '');   //接单市id
        $pageSize = (int)$request->get('page_size', 10);
        $date = (string)$request->get('created_at', '');      //时间选择
        $columns = ['id', 'real_name', 'phone', 'identity', 'status', 'company_id', 'city', 'licence_end', 'created_at', 'audit_time', 'auditor_id', 'taxi_status'];
        $list = $repository->getPageList($companyId, $province, $city, $realName, $phone, $identity, $status, $number, $date, $pageSize, $columns);
        $list['data'] = (new DriverTransformer())->transformList($list['data']);
        return $this->sendJson($list);
    }

    /**
     * @param Request $request
     * @param DriverRepository $repository
     * 代驾司机列表
     */
    public function getList(Request $request, DriverRepository $repository)
    {
        $realName = (string)$request->get('real_name', '');  //真实姓名
        $phone = (string)$request->get('phone', '');         //电话
        $identity = (string)$request->get('identity', '');    //身份证
        $companyId = (int)$request->get('company_id', '');   //公司id
        $status = (string)$request->get('status', '');       //状态
        $city = (int)$request->get('city', '');   //接单市id
        $pageSize = (int)$request->get('page_size', 10);
        $date = (string)$request->get('created_at', '');      //时间选择
        $province = (int)$request->get('province', '');   //接单省id
        $list = $repository->driving_list($companyId, $city, $realName, $phone, $identity, $status, $date, $pageSize, $province);
        return $this->sendJson($list);
    }

    /**
     * 司机详情
     * @param Request $request
     * @param DriverRepository $repository
     * @return DriverController|JsonResponse
     * @Author 姿势就是力量
     */

    public function getFirst(Request $request, DriverRepository $repository)
    {

        $driverId = (int)$request->get('driver_id', '');
        $driver = $repository->getFirst($driverId);
        $driver = (new DriverTransformer())->transform($driver);
        return $this->sendJson($driver);
    }


    /**
     * 保存司机信息
     * @param DriverRequest $request
     * @param DriverService $service
     * @return DriverController|JsonResponse
     * @Author 姿势就是力量
     */
    public function save(DriverRequest $request, DriverService $service)
    {

        $res = $service->saveData($request->post());
        if (!$res) return $this->sendError($service->getError());

        return $this->sendSuccess('保存成功');
    }

    /**
     * @param DriverRequest $request
     * @param DriverService $service
     * @return DriverController|JsonResponse
     * 代驾状态审核
     */
    public function saves(StateRequest $request, DriverService $service)
    {

        $id = (int)$request->post('id', '');
        $status = $request->post('status', '');
        $res = $service->saveStates($id, $status);
        if (!$res) return $this->sendError($service->getError());

        return $this->sendSuccess('保存成功');
    }

    /**
     * 保存司机信息
     * @param DriverRequest $request
     * @param DriverService $service
     * @return DriverController|JsonResponse
     * @Author 姿势就是力量
     */
    public function edit(DriverRequest $request, DriverService $service)
    {

        $res = $service->saveDatas($request->post());
        if (!$res) return $this->sendError($service->getError());

        return $this->sendSuccess('保存成功');
    }

    /**
     * 代驾司机详情
     */
    public function details(Request $request, DriverRepository $repository)
    {
        $driverId = (int)$request->post('driver_id');
        if (!$driverId) return $this->sendError('司机ID必传');
        $driver = $repository->details($driverId);
        $driver = (new DriverTransformer())->transformDetails_info($driver);
        return $this->sendJson($driver);
    }


    /**
     * 更新状态
     * @param StateRequest $request
     * @param DriverService $service
     * @return DriverController|JsonResponse
     * @Author 姿势就是力量
     */
    public function saveState(StateRequest $request, DriverService $service)
    {
        $id = (int)$request->post('id', '');
        $status = $request->post('status', '');
        $type = (string)$request->post('type', 1);
        if ($type == '1') {
            $res = $service->saveState($id, $status);
        } else {
            $res = $service->saveStates($id, $status);
        }
        if (!$res) return $this->sendError($service->getError());

        return $this->sendSuccess('保存成功');
    }

    /**
     * @param StateRequest $request
     * @param DriverService $service
     * @return DriverController|JsonResponse
     * 出租车状态
     */
    public function save_edit(StateRequest $request, DriverService $service)
    {
        $id = (int)$request->post('id', '');
        $status = $request->post('status', '');

        $res = $service->saveStatesss($id, $status);
        if (!$res) return $this->sendError($service->getError());

        return $this->sendSuccess('保存成功');
    }


    /**
     * 修改密码
     * @param PasswordRequest $request
     * @param DriverService $service
     * @return DriverController|JsonResponse
     * @Author 姿势就是力量
     */
    public function savePassword(PasswordRequest $request, DriverService $service)
    {
        $id = (int)$request->post('id', '');
        $password = (string)$request->post('password', '');
        $res = $service->savePassword($id, $password);
        if (!$res) return $this->sendError($service->getError());

        return $this->sendSuccess('修改成功');
    }


    /**
     * 导出司机
     * @param StateRequest $request
     * @param CarRepository $repository
     * @Author 姿势就是力量
     */

    public function carExcel(Request $request, DriverRepository $repository)
    {
        $user_id = $request->get('management_id', '');  //查所属公司
        $company = (new CompanyManagement())->where('management_id', $user_id)->get()->toArray();  //查所属公司
        $companyIds = array_column($company, 'company_id');   //查所属公司
        $realName = (string)$request->get('real_name', '');  //真实姓名
        $phone = (string)$request->get('phone', '');         //电话
        $identity = (string)$request->get('identity', '');    //身份证
        $companyId = (int)$request->get('company_id', '');   //公司id
        $status = (string)$request->get('status', '');       //状态
        $number = (string)$request->get('number', '');       //车牌号
        $province = (int)$request->get('province', '');   //接单省id
        $city = (int)$request->get('city', '');   //接单市id
        $date = (string)$request->get('created_at', '');      //时间选择
        $columns = ['id', 'real_name', 'company_id', 'phone', 'identity', 'status', 'city', 'licence_end', 'created_at', 'audit_time', 'auditor_id'];
        $list = $repository->excelPageList($companyId, $companyIds, $province, $city, $realName, $phone, $identity, $status, $number, $date, $columns);
        $list['data'] = (new DriverTransformer())->transformList($list);
        $car = $list['data'];

        $row = [
            [
                "id" => 'ID',
                "real_name" => '司机姓名',
                "phone" => '司机手机号',
                "identity" => '身份证号',
                "status" => "状态",
                "company" => '管理公司',
                "city" => '接单城市',
                "car" => '绑定车辆号牌',
                "created_at" => '创建时间'
            ]
        ];

        return Excel::download(new  CarDriverExport($row, $car), '司机.xls');

    }

    /**
     * @param Request $request
     * @param DriverRepository $repository
     * 代驾导出
     */
    public function carExcels(Request $request, DriverRepository $repository)
    {
        $user_id = $request->get('management_id', '');  //查所属公司
        $company = (new CompanyManagement())->where('management_id', $user_id)->get()->toArray();  //查所属公司
        $companyIds = array_column($company, 'company_id');   //查所属公司
        $realName = (string)$request->get('real_name', '');  //真实姓名
        $phone = (string)$request->get('phone', '');         //电话
        $identity = (string)$request->get('identity', '');    //身份证
        $companyId = (int)$request->get('company_id', '');   //公司id
        $status = (string)$request->get('status', '');       //状态
        $city = (int)$request->get('city', '');   //接单市id
        $date = (string)$request->get('created_at', '');      //时间选择
        $list = $repository->carExcels($companyId, $companyIds, $city, $realName, $phone, $identity, $status, $date);
        $row = [
            [
                "driver_id" => 'ID',
                "driver_name" => '司机姓名',
                "phone" => '司机手机号',
                "identity" => '身份证号',
                "status" => "状态",
                "company_name" => '管理公司',
                "merger_name" => '接单城市',
                "created_at" => '创建时间'
            ]
        ];

        return Excel::download(new  DriverExports($row, $list), '代驾司机.xls');
    }
}
